package com.c.ctgproject.wiget.common

import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import org.w3c.dom.Text

class ListSelectDialog(private val mTitle: String, private val mAdapter: RecyclerView.Adapter<*>, private val isSingle: Boolean = false) : DialogFragment() {
    private var mTitleTV: TextView? = null
    private var mRecyclerView: RecyclerView? = null
    private var mDeleteTV: ImageView? = null
    private var mConfirmBTN: Button? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.dialog_list_select, container, false)
        mTitleTV = view.findViewById(R.id.mTitleTV)
        mRecyclerView = view.findViewById(R.id.mRecyclerView)
        mDeleteTV = view.findViewById(R.id.mDeleteTV)
        mConfirmBTN = view.findViewById(R.id.mConfirmBTN)
        mTitleTV?.text = mTitle
        mRecyclerView?.adapter = mAdapter
        mRecyclerView?.layoutManager = LinearLayoutManager(requireActivity())
        mDeleteTV?.setOnClickListener {
            dismiss()
        }
        if (isSingle) {
            mConfirmBTN?.visibility = View.GONE
        }
        mConfirmBTN?.setOnClickListener {
            dismiss()
        }

        return view
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.CommonDialog)
    }


    override fun onStart() {
        super.onStart()
        dialog?.window?.attributes?.windowAnimations = R.style.alphAnimation
        dialog?.setCanceledOnTouchOutside(false)
        val display = DisplayMetrics()
        requireActivity().windowManager.defaultDisplay.getMetrics(display)
        //设置宽高占比
        val params = dialog!!.window!!.attributes
        //此处占满屏幕宽度
        params.height = RelativeLayout.LayoutParams.WRAP_CONTENT
        //从底部出现
//        params.gravity = Gravity.BOTTOM
        //高度自己定义
        dialog!!.window!!.setLayout(display.widthPixels / 5 * 4, RelativeLayout.LayoutParams.WRAP_CONTENT)
    }
}